package com.by_man.repository;

import android.graphics.Bitmap;
import android.os.Handler;
import android.widget.ImageView;
import android.widget.Toast;

import com.android.volley.RequestQueue;
import com.by_man.presenter.module_health_records.image.BitmapManager;

import org.json.JSONArray;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.HashMap;


/**
 * 管理本地数据
 * Created by man on 2017-03-03.
 */
public class LocalDataManager {
    public static BackgroundService service;
    public static NetworkDataManager manager;

    /***************************************************************
     * 血压
     ***************************************************************/
    // 返回用户一条血压记录
    public static JSONArray getOneBloodPressureRecord() {
        if (null == UserInfos.One_BloodPressure_Record) {
            showMessage("暂无数据，即将请求服务器，请稍后再试!");
            // 没有数据，请求一次
            manager.getOneBloodPressureRecordByNetwork();
        }
        return UserInfos.One_BloodPressure_Record;
    }

    // 返回用户所有血压记录
    public static JSONArray getAllBloodPressureRecord() {
        if (null == UserInfos.All_BloodPressure_Record) {
            showMessage("暂无数据，即将请求服务器，请稍后再试!");
            // 没有数据，请求一次
            manager.getAllBloodPressreRecordByNetword();
        }
        return UserInfos.All_BloodPressure_Record;
    }

    // 上传血压心率记录，并更新
    public static void updateBloodPressureRecord(String record){
        // 上传手动输入记录
        manager.uploadManualBloodPressreRecord(record);
        // 更新血压信息
        manager.getOneBloodPressureRecordByNetwork();
        manager.getAllBloodPressreRecordByNetword();
    }


    /***************************************************************
     * 血糖
     ***************************************************************/
    public static JSONArray getThreeBloodSugarRecord() {
        if (null == UserInfos.Three_BloodSugar_Record) {
            showMessage("暂无数据，即将请求服务器，请稍后再试!");
            manager.getThreeBloodSugarRecordByNetwork();
        }

        return UserInfos.Three_BloodSugar_Record;
    }

    public static JSONArray getAllBloodSugarRecord() {
        if (null == UserInfos.All_BloodSugar_Record) {
            showMessage("暂无数据，即将请求服务器，请稍后再试!");
            manager.getAllBloodSugarRecordByNetwork();
        }

        return UserInfos.All_BloodSugar_Record;
    }

    // 上传血糖信息，并更新
    public static void updateBloodSugarRecord(String record) {
        manager.uploadManualBloodSugarRecord(record);
        manager.getThreeBloodSugarRecordByNetwork();
        manager.getAllBloodSugarRecordByNetwork();
    }

    /***************************************************************
     * 健康建议
     ***************************************************************/
    public static JSONArray getAllHealthAdviceRecord() {
        if (null == UserInfos.All_HealthAdvice_Record) {
            showMessage("暂无数据，即将请求服务器，请稍后再试!");
            manager.getAllHealthAdviceRecordByNetwork();
        }

        return UserInfos.All_HealthAdvice_Record;
    }

    /***************************************************************
     * 健康顾问
     ***************************************************************/
    public static JSONArray getAllDoctor() {
        if (null == UserInfos.All_Doctor) {
            showMessage("暂无数据，即将请求服务器，请稍后再试!");
            manager.getAllDoctorByNetwork();
        }

        return UserInfos.All_Doctor;
    }


    /***********************************************************************************************
     * 载入网络图像
     ***********************************************************************************************/


    /***********************************************************************************************
     * 会员活动
     ***********************************************************************************************/
    public static JSONArray getAllNewActivity() {
        if (null == UserInfos.All_New_Activity) {
            showMessage("暂无数据，即将请求服务器，请稍后再试!");
            manager.getAllNewActivityByNetwork();
        }

        return UserInfos.All_New_Activity;
    }

    public static JSONArray getAllLatelyActivity() {
        if (null == UserInfos.All_Lately_Activity) {
            showMessage("暂无数据，即将请求服务器，请稍后再试!");
            manager.getAllLatelyActivityByNetwork();
        }

        return UserInfos.All_Lately_Activity;
    }

    public static JSONArray getNewActivityDetail(String name) {
        JSONArray jsonArray = UserInfos.All_New_Activity_Detail.get(name);
        if (null == jsonArray) {
            showMessage("暂无数据，即将请求服务器，请稍后再试!");
            manager.getOneNewActivityDetailByNetworkByNetwork(name);
        }
        return jsonArray;
    }

    public static JSONArray getLatelyDetail(String name) {
        JSONArray jsonArray = UserInfos.All_Lately_Activity_Detail.get(name);
        if (null == jsonArray) {
            showMessage("暂无数据，即将请求服务器，请稍后再试!");
            manager.getOneLatelyActivityDetailByNetworkByNetwork(name);
        }
        return jsonArray;
    }

    public static JSONObject getActivityNumber(String name) {
        JSONObject jsonObject = UserInfos.Activity_Number.get(name);
        if (jsonObject == null) {
            showMessage("暂无数据，即将请求服务器，请稍后再试!");
            manager.getActivityNumberByNetwork(name);
        }
        return jsonObject;
    }

    /***********************************************************************************************
     * 上传文件
     ***********************************************************************************************/
    public static void uploadFiles(String url, ArrayList<HashMap<String, String[]>> files, HashMap<String, String> params) {

    }

    /***********************************************************************************************
     * 服务电话、个人信息
     ***********************************************************************************************/
    public static JSONArray getServicePhone(){
        if (null == UserInfos.Service_Phone) {
            showMessage("暂无数据，即将请求服务器，请稍后再试!");
            manager.getServicePhoneByNetwork();
        }
        return UserInfos.Service_Phone;
    }

    public static void updateSelfInfos(String json, Handler handler) {
        manager.updateSelfInfos(json,handler);
    }

    // 显示必要的提示信息
    public static void showMessage(String msg){
        Toast.makeText(service,msg,Toast.LENGTH_SHORT).show();
    }

    // 返回RequestQueue执行必要的单独的网络请求
    public static RequestQueue getRequestQueue() {
        return service.getQueue();
    }

    public static NetworkDataManager getNetworkDataManager() {
        return manager;
    }

}
